<?php

declare(strict_types=1);

use Phinx\Db\Adapter\MysqlAdapter;
use think\migration\Migrator;

class CreateSearchHistoryTable extends Migrator
{
    public function change(): void
    {
        $this->table('search_history', [
            'comment' => '用户搜索记录',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'null' => true,
                'signed' => false,
                'comment' => '搜索用户 ID（可为空）',
            ])
            ->addColumn('seller_org_id', 'integer', [
                'null' => true,
                'signed' => false,
                'comment' => '卖家组织 ID（管理员/客服代查时记录）',
            ])
            ->addColumn('buyer_org_id', 'integer', [
                'null' => true,
                'signed' => false,
                'comment' => '买家组织 ID（商城端搜索时记录）',
            ])
            ->addColumn('keyword', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '原始搜索关键词',
            ])
            ->addColumn('model_keyword', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '型号关键词',
            ])
            ->addColumn('category_id', 'integer', [
                'null' => true,
                'signed' => false,
                'comment' => '分类 ID',
            ])
            ->addColumn('category_name', 'string', [
                'limit' => 150,
                'default' => '',
                'comment' => '分类名称',
            ])
            ->addColumn('brand_id', 'integer', [
                'null' => true,
                'signed' => false,
                'comment' => '品牌 ID',
            ])
            ->addColumn('brand_name', 'string', [
                'limit' => 150,
                'default' => '',
                'comment' => '品牌名称',
            ])
            ->addColumn('region_id', 'integer', [
                'null' => true,
                'signed' => false,
                'comment' => '区域 ID',
            ])
            ->addColumn('region_name', 'string', [
                'limit' => 150,
                'default' => '',
                'comment' => '区域名称或路径',
            ])
            ->addColumn('warehouse_code', 'string', [
                'limit' => 64,
                'default' => '',
                'comment' => '货仓号（按日变更）',
            ])
            ->addColumn('result_count', 'integer', [
                'default' => 0,
                'comment' => '命中数量',
            ])
            ->addColumn('client_type', 'string', [
                'limit' => 20,
                'default' => 'seller',
                'comment' => '来源端：seller|admin|buyer 等',
            ])
            ->addColumn('extra', 'text', [
                'limit' => MysqlAdapter::TEXT_REGULAR,
                'null' => true,
                'comment' => '额外过滤条件(JSON)',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '软删除时间',
            ])
            ->addIndex(['user_id'])
            ->addIndex(['seller_org_id'])
            ->addIndex(['buyer_org_id'])
            ->addIndex(['keyword'])
            ->addIndex(['model_keyword'])
            ->addIndex(['warehouse_code'])
            ->create();
    }
}
